"use strict";
const chp = require("child_process");
const p = require("path");
const { Controller } = require("egg");
const fs = require("fs-extra");
const { PassThrough } = require("stream");
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    // ctx.body = "<h1>film manage api site!</h1>";
    await ctx.render("main/index.html");
  }
  async download() {
    const { ctx } = this;
    let fp = p.join(this.app.baseDir, "/app/public/tmpfile");
    let files = fs.readdirSync(fp);
    await ctx.render("main/download.html", { files });
  }
  async detail() {
    const { ctx } = this;
    await ctx.render("main/detail.html");
  }

  async getConfigSpace() {
    let space = this.config.space;
    this.ctx.success({ data: space, msg: "获取配置成功" });
  }
  async getIndexData() {
    const { ctx, service } = this;
    let ret = await service.home.getIndexData();
    ctx.success(ret);
  }
  async test() {
    console.log("测试disposeTeleplayTask ");
    this.app.sseMessage({kk: new Date()});
    // ctx.body = "<h1>film manage api site!</h1>";
    // this.ctx.app.messenger.sendToApp("add-tv", {});
    // console.log(this.ctx.helper.md5("kkkl"));
    this.ctx.success();
  }
  async sse() {
    let { ctx, app } = this;
    ctx.set({
      "Content-type": "text/event-stream",
      "Cache-Control": "no-cache",
      Connection: "keep-alive",
    });
    let sseStream = new PassThrough(); // sse Stream
    app.sseStream = sseStream;
    ctx.status = 200;
    sseStream.on("close", () => {
      console.log("sseStream 连接断开");
    });
    ctx.body = app.sseStream;
  }
}

module.exports = HomeController;
